// ActionScript file

/*
    DESIGN NOTES:
    
      LINKING COLOR SQUARES IN MULTIPLE COLORS:
      
      3x11 containers for 3 different backgrounds and 11 primary colors per.  Only 1/3 of the background data is visible at 
      any given time.
      
      Each of the 11 primary color displays is a container of sprites with childAt(i).  Each child is a sprite object.
      When one is selected from any of the 11 displays, its underlying data is accessed to see what other colors it has
      been called.  We search through all of the children in these containers which is 11*O(n) until we find the
      child with the matching chip number, then we redraw the child with a highlighting border.
      
      Data:
      
      Have 454*3 data items, one for each chip.  Each chip contains all the colors it was named, and the count of the times
      named that color.  We traverse all the data items and add their chip number and corresponding counts to each of the 
      11 containers.  We then sort the containers based on count.
      
      ALTERNATIVE IDEA:
      
        pre-handle the data such that each item in a container holds chip number, count in current container, all other
        containers it is in (including its position, eg childAt(i) in those containers).  This will allow for very fast
        linking on mouse over.  The difficult part is parsing the data to arrive at this state, but it's doable.



*/


package
{
  import fl.controls.CheckBox;
  import fl.controls.ComboBox;
  import fl.data.DataProvider;
  
  import flare.display.TextSprite;
  
  import flash.display.Sprite;
  import flash.events.*;

  public class ComboBoxes extends Sprite
  {
    private var bgSelectCB:ComboBox;
    
    private var bgSelectCB_items:Array;
    
    private var bgDisplayCB:CheckBox;

    public function ComboBoxes(){
      
      bgSelectCB_items = [ 
        {label:"Black background", data:"black"}, 
        {label:"Gray background", data:"gray"}, 
        {label:"White background", data:"white"},
      ]; 
      
      var bgSelect_label:TextSprite = new TextSprite("Background used in survey:");
      bgSelect_label.color = 0xffffff;
      bgSelect_label.alpha = 0.6;
      bgSelect_label.x = 1495;
      bgSelect_label.y = 80;
      bgSelect_label.font = "Calibri";
      bgSelect_label.size = 13;
      addChild(bgSelect_label);
      
      bgSelectCB = new ComboBox();
      bgSelectCB.rowCount = 3;
      bgSelectCB.dataProvider = new DataProvider(bgSelectCB_items);
      bgSelectCB.x = 1495;
      bgSelectCB.y = 105;
      bgSelectCB.width = 150;
      addChild(bgSelectCB);
      bgSelectCB.dropdown.rowHeight = 30;
      //bgSelectCB.dropdownWidth = 90;
      
      bgSelectCB.addEventListener(Event.CHANGE, bgSelect_Change);
      
      bgDisplayCB = new CheckBox();
      bgDisplayCB = new CheckBox();
			bgDisplayCB.x = 1488;
			bgDisplayCB.y = 140;
			bgDisplayCB.selected = false;
			bgDisplayCB.label = "";
			addChild(bgDisplayCB);
			
			bgDisplayCB.addEventListener(Event.CHANGE, bgDisplayHandler);
			
			var bgDisplay_label:TextSprite = new TextSprite("Display actual background");
			bgDisplay_label.font = "Calibri";
			bgDisplay_label.size = 13;
			bgDisplay_label.color = 0xffffff;
			bgDisplay_label.alpha = 0.6;
			bgDisplay_label.x = 1510;
			bgDisplay_label.y = 140;
			addChild(bgDisplay_label);
      
    }

    // handles checkbox that determines whether to display actual color used in background in surveys
    protected function bgDisplayHandler(event:Event):void{
      dispatchEvent(new Event("bg display change"));
    }

    // handles combobox that determines which background's results to display
    protected function bgSelect_Change(event:Event):void{
      dispatchEvent(new Event("bg select change"));
    }
    
    private function findItemIndex (element:ComboBox, dataString:String):int {
      var index:int = 0;
      for (var i:int = 0; i < element.length; i++) {
        if (element.getItemAt(i).data.toString() == dataString) {
          index = i;
          break;
        }
        else {}
      }
      return index;
    }

    public function setDataMode(mode:String):void{
      bgSelectCB.selectedIndex = this.findItemIndex(bgSelectCB, mode);
      dispatchEvent(new Event(Event.CHANGE));
    }

    public function getDataMode():String{
      
      if (bgSelectCB.selectedLabel == "Black background")
        return "black";
      else if (bgSelectCB.selectedLabel == "Gray background")
        return "gray";
      else if (bgSelectCB.selectedLabel == "White background")
        return "white";
  
      return "error";
    }
    
    public function getBGDisplay():Boolean{
      return bgDisplayCB.selected;
    }
    
    public function getBGSelectColor():String{
      var index:int = bgSelectCB.selectedIndex;
      return bgSelectCB.getItemAt(index).data.toString();
    }
  }
}